package cn.db101.api.complex.model

import cn.hutool.core.map.MapUtil
import cn.db101.api.complex.enumration.ResultCodeEnum
import org.springframework.util.StringUtils

class WebResult<T> {

    var code: Int? = null
    var msg: String? = null
    var data: T? = null
    val sign: String? = null

    companion object {
        fun <T> wrapSuccessResult(data: T): WebResult<T> {
            val result = WebResult<T>()
            result.data = data
            result.code = ResultCodeEnum.SUCCESS.code
            result.msg = ResultCodeEnum.SUCCESS.msg
            return result
        }

        fun <T> wrapSuccessResult(): WebResult<T> {
            val result = WebResult<T>()
            result.code = ResultCodeEnum.SUCCESS.code
            result.msg = ResultCodeEnum.SUCCESS.msg
            return result
        }

        fun wrapSuccessResult(key: String?, data: Any?): WebResult<Map<String?, Any?>> {
            val result = WebResult<Map<String?, Any?>>()
            result.data = MapUtil.builder(key, data).build()
            result.code = ResultCodeEnum.SUCCESS.code
            result.msg = ResultCodeEnum.SUCCESS.msg
            return result
        }

        fun <T> wrapErrorResult(codeEnum: ResultCodeEnum): WebResult<T> {
            val result = WebResult<T>()
            result.code = codeEnum.code
            result.msg = codeEnum.msg
            return result
        }

        fun <T> wrapErrorResult(codeEnum: ResultCodeEnum, msg: String?): WebResult<T> {
            val result = WebResult<T>()
            result.code = codeEnum.code
            result.msg = msg
            return result
        }

        fun <T> wrapErrorResultMsgPlus(codeEnum: ResultCodeEnum, msg: String?): WebResult<T> {
            val result = WebResult<T>()
            result.code = codeEnum.code
            result.msg = if (StringUtils.isEmpty(msg)) codeEnum.msg else msg
            return result
        }
    }
}
